package cn.bdqn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bdqn.dao.Deptdao;
import cn.bdqn.pojo.Dept;
import cn.bdqn.pojo.Page;
import cn.bdqn.service.Deptservice;
import cn.bdqn.service.DeptserviceImpl;

public class DeptServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		Deptservice deptservice = new DeptserviceImpl();

		if (id.equals("1")) {
			String index = request.getParameter("pageIndex");
			int pageIndex = Integer.parseInt(index);
			Page page = new Page();
			int counts = deptservice.getCounts();
			// 总页数
			page.setCounts(counts);
			page.setPageIndex(pageIndex);
			int pageSize = 2;
			page.setPageSize(pageSize);
			List<Dept> list = deptservice.getPageList(pageIndex, pageSize);
			request.setAttribute("list", list);
			request.setAttribute("page", page);
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
		} else if (id.equals("2")) {
			String deptt = request.getParameter("deptno");
			int dept = Integer.parseInt(deptt);
			Dept dept2 = deptservice.getDept(dept);
			request.setAttribute("dept", dept2);
			request.getRequestDispatcher("/dept.jsp")
					.forward(request, response);

		} else if (id.equals("3")) {
			String deptt = request.getParameter("deptn");
			int dept = Integer.parseInt(deptt);
			deptservice.delete(dept);
			request.getRequestDispatcher("/dept1.jsp").forward(request,
					response);
		} else if (id.equals("4")) {
			String deptnn = request.getParameter("deptnn");
			String deptnnn = request.getParameter("deptnnn");
			String deptnnnn = request.getParameter("deptnnnn");
			int dept = Integer.parseInt(deptnn);
			deptservice.add(dept, deptnnn, deptnnnn);

			request.getRequestDispatcher("/dept2.jsp").forward(request,
					response);
		} else if (id.equals("5")) {
			String deptno = request.getParameter("deptno");
			String dname = request.getParameter("dname");
			String loc = request.getParameter("loc");
			int deptno1 = Integer.parseInt(deptno);
			Dept dept = new Dept();
			dept.setDeptno(deptno1);
			dept.setDname(dname);
			dept.setLoc(loc);

			if (deptservice.update(dept)) {
				request.getRequestDispatcher("/index.jsp").forward(request,
						response);

			}

		}

	}
}
